/**
 * @author   zhangyp
 * @created  2013-12-28 下午7:39:59
 * @email    zviolet@163.com
 * TODO
 */
package com.hyacinth.wapper;

import com.hyacinth.utils.Assert;
import lombok.Data;

/**
 * 分页对象 
 */
@Data
public class PageCond {
	
	//分页查询条件，返回记录的起始记录号
	private Integer begin = 0;
	//分页查询条件，返回记录数长度
	private Integer length = 10;
	//分页状态信息，总记录数
	private Long count = 0L;
	//分页状态信息，当前页号
	private Integer currentPage = 0;
	//分页状态信息，当前页记录数
	private Integer size = 0;

	public static PageCond ofBeginAndLength(Integer begin, Integer length){
		Assert.notNull(begin, "The begin row cannot be empty");
		Assert.notNull(begin, "The length of records cannot be empty");
		PageCond pageCond=new PageCond();
		try {
			pageCond.setBegin(begin);
		} catch (Exception e) {
			pageCond.setBegin(0);
		}
		try {
			pageCond.setLength(length);
		} catch (Exception e) {
			pageCond.setLength(10);
		}
		pageCond.setCurrentPage(pageCond.begin/pageCond.length);
		return pageCond;
	}
	
	public static PageCond ofPageNumAndSize(Integer pageNum, Integer pageSize){
		Assert.notNull(pageNum, "The pageNum cannot be empty");
		PageCond pageCond = new PageCond();
		if (pageSize == null) {
			pageSize = 10;
		}
		try {
			pageCond.setLength(pageSize);
		} catch (Exception e) {
			pageCond.setLength(10);
		}
		try {
			pageCond.setBegin((pageNum-1)*pageCond.getLength());
		} catch (Exception e) {
			pageCond.setBegin(0);
		}
		pageCond.setCurrentPage(pageCond.begin / pageCond.length);
		return pageCond;
	}

}
